﻿
/*
 仓位
*/

$(function () {
    $('#storageTreeGrid').treegrid({

        //title: 'TreeGrid',
        fitColumns: true,
        rownumbers: true,
        resizable: true,
        idField: 'Id',
        treeField: 'Name',
        toolbar: '#storageToolbar',
        singleSelect: true,
        checkbox:true,
        columns: [[
               { title: 'Id', field: 'Id', align: 'left', width: 10, hidden: true },
               { title: '名称', field: 'Name', align: 'left', width: 200 },
               { title: '编号', field: 'Number', align: 'left', width: 100 },

               { title: '是否最末级', field: 'IsLeaf', align: 'left', width: 100, formatter: getIsIsLeaf },
               { title: '管理部门', field: 'ManageDept', align: 'left', width: 200, formatter: getManageDeptName },
               { title: '层级', field: 'NodeLevel', align: 'left', width: 100 },
               { title: '描述', field: 'Description', align: 'left', width: 300 },
               { title: 'ParentId', field: "Parent", align: 'left', width: 50, formatter: getParentId, hidden: true },
        ]]
    });
    loadStorages();
});

function getManageDeptName(value, rowData, index) {
    if (rowData.ManageDept != null) {
        return gFormatDeptName(rowData.ManageDept.Name);
    } else
        return null;
}
function getIsEmpty(value, rowData, index) {
    if (value) {
        return "是";
    } else {
        return "否";
    }
};

function getIsIsLeaf(value, rowData, index) {
    if (value) {
        return "是";
    } else {
        return "否";
    }
};

function getParentId(value, rowData, index) {
    if (rowData.Parent != null) {
        return rowData.Parent.Id;
    } else
        return 0;
}

function loadStorages() {
    $.ajax({
        url: "/BaseData/LoadStorageList",
        type: "POST",
        dataType: "json",
        //async:false,
        success: function (req) {
            if (req.IsSuccess) {//成功填充数据
                try {
                    var storagesObjs = JSON.parse(req.Data);
                    var ss = gStorageTreeConvert(storagesObjs);
                }
                catch (ex) {
                    alert(ex + "2");
                }
                $('#storageTreeGrid').treegrid('loadData', ss);
            }
            else {
                alert("获取数据失败");
            }
        }
    });
}

function convert(storages) {
    try {

        var ss = [];
        var count = storages.length;

        for (var i = 0; i < count; i++) {

            var s = storages[i];

            if (!s.IsLeaf) {
                //alert('a :' + s.Name);
                s.children = [];

                for (var j = 0; j < count; j++) {
                    var f = storages[j];
                    if (f.Parent != null && f.Parent.Id == s.Id) {
                        var c = {};
                        c.Id = f.Id;
                        c.Name = f.Name;
                        c.Number = f.Number;
                        c.Description = f.Description;
                        c.NodeLevel = f.NodeLevel;

                        s.children.push(f);
                    }
                }
            }
            if (s.NodeLevel == 1) {
                ss.push(s);
            }
        }
        return ss;
    } catch (ex) {
        alert(ex);
    }
}

var mStorageViewDlg;

function btnAddRootStorageAction() {
    //var ctrType = 0;//0=新增；1=编辑（查看）;
    var storageId = 0;

    try {

        storageId = 0;

        mStorageViewDlg = art.dialog({
            id: 'diaFormStorage',
            title: '仓储位置',
            content: "<iframe scrolling='yes' frameborder='0' src='/BaseData/StorageView?storageId=" + storageId + "&ctrType=0' style='width:400px;height:320px;'/>",
            lock: false,
            initFn: function () {
            }
        });
    } catch (ex) {
        alert(ex);
    }
}

function btnAddChildStorageAction() {

    //var ctrType = 0;//0=新增；1=编辑（查看）;
    var storageId = 0;

    try {

        var rows = $("#storageTreeGrid").datagrid("getSelected");
        if (rows) {
            storageId = rows.Id;
        }

        mStorageViewDlg = art.dialog({
            id: 'diaFormStorage',
            title: '仓储位置',
            content: "<iframe scrolling='yes' frameborder='0' src='/BaseData/StorageView?storageId=" + storageId + "&ctrType=0' style='width:400px;height:320px;'/>",
            lock: false,
            initFn: function () {
            }
        });
    } catch (ex) {
        alert(ex);
    }
}



function btnEditStorageAction() {
    var storageId = 0;
    var rows = $("#storageTreeGrid").datagrid("getSelected");

    if (rows) {
        storageId = rows.Id;

        mStorageViewDlg = art.dialog({
            id: 'diaFormStorage',
            title: '仓储位置',
            content: "<iframe scrolling='yes' frameborder='0' src='/BaseData/StorageView?storageId=" + storageId + "&ctrType=1' style='width:400px;height:360px;'/>",
            lock: false,
            initFn: function () {
            }
        });
    }

}

function btnDelStorageAction() {

    var rows = $("#storageTreeGrid").datagrid("getSelected");

    if (rows) {
        $.messager.confirm('操作提示', "确定删除： " + rows.FullName + " 的仓位?", function (r) {
            if (r) {
                $.ajax({
                    url: "/BaseData/DeleteStorage",
                    type: "POST",
                    dataType: "json",
                    data: { "storageId": rows.Id },
                    async: false,
                    success: function (req) {
                        if (req.IsSuccess) {
                            loadStorages();
                        } else {
                            $.messager.alert('提示', req.Data);
                        }

                        return;
                    }
                });
            }
        });
    }
}